<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>预收款管理</title>
<link href="${basePath }/css/ui.css" rel="stylesheet" type="text/css">
<jsp:include page="/header.jsp"></jsp:include>
<script type="text/javascript">
	$(function() {
		$(window).keydown(function(event){
			switch(event.keyCode) {
				case 13:{
					scan();
				}
			}
		});
	})
	
	function scan() {
		var code = $("#qrcode").val();
		var flag = true;
		$.each($("#send_table").find("tr:gt(0)"), function(i,n){
			var tempCode = $(n).find("td:eq(e)").text();
			if(tempCode==code) {
				flag = false;
				return false;
			}
		})
		if(code==""||code==null) $.jqalert("请输入二维码！！", "提示");
		else if(!flag) $.jqalert("请勿输入重复二维码！！", "提示");
		else {
			$("#send_form").find("input[name='send_code']").val(code);
			$.ajax({
				url:"${basePath}/trade/advance_scan.htm",
				type:"post",
				data:$("#send_form").serialize(),
				dataType:"html"
			}).done(function(data){
				if($.trim(data)!='') {
					var tr = $("<tr></tr>");
					tr.html(data);
					$("#send_table").append(tr);
					resetIndex();
					fillOldPrice(tr);
				} else {
					$.jqalert("未找到商品！！", "提示");
				}
			})
		}
	}
	
	function resetIndex() {
		$.each($("#send_table").find("tr:gt(0)"),function(i,n){
			$(n).find("td:eq(0)").text(i+1);
		});
	}
	
	function fillOldPrice(tr) {
		var productId = tr.find("input[name='product_id']").val();
		$.each($("#send_form").find("tr:gt(0)"), function(i, n){
			var pid = $(n).find("input[name='send_product_id']").val();
			if(pid==productId) {
				tr.find("td:eq(6)").text($(n).find("td:eq(5)").text());
			}
		})
	}
	
	function confirmAndSend() {
		var codes = new Array();
		$.each($("#send_table").find("tr:gt(0)"), function(i, n){
			var code = {};
			$.each($(n).find("td"), function(i, n){
				var value = "";
				if(i==8) value = $(n).find("input[name='now_price']").val();
				else value = $(n).text();
				eval("code."+$(n).attr("title")+"=\""+value+"\"");
			});
			code.product_id = $(n).find("input[name='product_id']").val();
			codes.push(code);
		})
		$.ajax({
			url:"${basePath}/trade/advance_send.htm",
			type:"post",
			data:{codes:JSON.stringify(codes), orderId:"${param.orderId}", customerId:"${info.customer.id}"},
			dataType:"json"
		}).done(function(data){
			$.jqalert(data.msg, "提示", function() {
				if(data.isSuccess) window.location = window.location;
			})
		})
	}
</script>
</head>
<body>
<div class="cont">
	<a href="${basePath }/trade/index.htm?module=advance" class="yellow_btn btn">返回</a>
	
	<h2 class="title">客人信息：</h2>
	<div class="b_div">
		<p class="clearfix">
			<span class="info_s fl">客户名称：${info.customer.name }</span>
			<span class="info_s fl">联系电话：${info.customer.tel }</span></span>
			<span class="info_s fl">传真：${info.customer.fax }</span>
			<span class="info_s fl">邮箱：${info.customer.email }</span>
		</p>
		<p class="clearfix">
			<span class="info_s fl">公司地址：${info.customer.comaddress }</span>
		</p>
		<p class="clearfix">
			<span class="info_s fl">送货地址：${info.customer.address }</span>
		</p>
	</div>
	
    <h2 class="title">订购商品列表：</h2>
    <div class="ui">
    <form id="send_form">
        <table class="table_list">
            <tr class="">
              <th class="">序号</th>
              <th>品名</th>
              <th class="">年份</th>
              <th class="">批次</th>
              <th class="">类型</th>
              <th class="">预收价格（元）</th>
              <th class="">已发货数量</th>
            </tr>
            <input type="hidden" name="send_code">
            <c:forEach items="${info.products }" var="product" varStatus="status">
            <tr class="">
            	<input type="hidden" name="send_product_id" value="${product.product_id}">
              <td class="">${status.index+1 }</td>
              <td class="">${product.product_name }</td>
              <td class="">${product.productionyear }</td>
              <td class="">${product.batch }</td>
              <td class="">${product.unit }</td>
              <td class="">${product.advance_price }</td>
              <td class="">${product.send_num }</td>
            </tr>
            </c:forEach>
        </table>
        </form>
    </div>
    
    <h2 class="title">未发货商品：</h2>
    <div class="ui">
    
    	<div class="label_box">
            <span class="sel_label fl">扫描商品二维码：</span>
            <input type="text" id="qrcode" class="text_input fl">
        </div>
    
        <table id="send_table" class="table_list">
            <tr class="">
              <th class="">序号</th>
              <th>品名</th>
              <th>二维码</th>
              <th class="">年份</th>
              <th class="">批次</th>
              <th class="">类型</th>
              <th class="">预收价格（元）</th>
              <th>现价（元）</th>
              <th class="">成交价格（元）</th>
            </tr>
        </table>
    </div>
    
    <div class="text_c">
    	<a href="javascript:;" class="yellow_btn btn" onclick="confirmAndSend();">确认发货</a>
    </div>
    
	</div>
	</body>
	</html>

